package cdu.hjx.project.care.handler;

import cdu.hjx.project.care.service.PatientsService;
import cdu.hjx.project.pojo.PatientDto;
import cdu.hjx.project.utils.CommonResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@CrossOrigin
@RestController
@RequestMapping("/patients")
public class PatientsHandler {

    @Resource
    private PatientsService patientsService;

    /**
     * 添加患者信息
     * @param patientDto 患者信息和其就诊信息的集合
     * @return CommonResult 统一返回类
     */
    @PostMapping("/add")
    public CommonResult addPatients(@RequestBody PatientDto patientDto) {
        // 添加基础信息
        try {
            boolean added = patientsService.addPatients(patientDto.getPatients(), patientDto.getPatientsDetail());
            return added ? CommonResult.success(patientDto) : CommonResult.error(201, "添加患者信息失败");
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.error(500, "系统异常,添加失败,请联系管理员.");
        }

    }
}
